{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Count of Matches in Tournament"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #math #simulation"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数学 #模拟"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: numberOfMatches"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #比赛中的配对次数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一个整数 <code>n</code> ，表示比赛中的队伍数。比赛遵循一种独特的赛制：</p>\n",
    "\n",
    "<ul>\n",
    "\t<li>如果当前队伍数是 <strong>偶数</strong> ，那么每支队伍都会与另一支队伍配对。总共进行 <code>n / 2</code> 场比赛，且产生 <code>n / 2</code> 支队伍进入下一轮。</li>\n",
    "\t<li>如果当前队伍数为 <strong>奇数</strong> ，那么将会随机轮空并晋级一支队伍，其余的队伍配对。总共进行 <code>(n - 1) / 2</code> 场比赛，且产生 <code>(n - 1) / 2 + 1</code> 支队伍进入下一轮。</li>\n",
    "</ul>\n",
    "\n",
    "<p>返回在比赛中进行的配对次数，直到决出获胜队伍为止。</p>\n",
    "\n",
    "<p> </p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>n = 7\n",
    "<strong>输出：</strong>6\n",
    "<strong>解释：</strong>比赛详情：\n",
    "- 第 1 轮：队伍数 = 7 ，配对次数 = 3 ，4 支队伍晋级。\n",
    "- 第 2 轮：队伍数 = 4 ，配对次数 = 2 ，2 支队伍晋级。\n",
    "- 第 3 轮：队伍数 = 2 ，配对次数 = 1 ，决出 1 支获胜队伍。\n",
    "总配对次数 = 3 + 2 + 1 = 6\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>n = 14\n",
    "<strong>输出：</strong>13\n",
    "<strong>解释：</strong>比赛详情：\n",
    "- 第 1 轮：队伍数 = 14 ，配对次数 = 7 ，7 支队伍晋级。\n",
    "- 第 2 轮：队伍数 = 7 ，配对次数 = 3 ，4 支队伍晋级。 \n",
    "- 第 3 轮：队伍数 = 4 ，配对次数 = 2 ，2 支队伍晋级。\n",
    "- 第 4 轮：队伍数 = 2 ，配对次数 = 1 ，决出 1 支获胜队伍。\n",
    "总配对次数 = 7 + 3 + 2 + 1 = 13\n",
    "</pre>\n",
    "\n",
    "<p> </p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= n &lt;= 200</code></li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [count-of-matches-in-tournament](https://leetcode.cn/problems/count-of-matches-in-tournament/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [count-of-matches-in-tournament](https://leetcode.cn/problems/count-of-matches-in-tournament/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['7', '14']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfMatches(self, n: int) -> int:\n",
    "        res = 0\n",
    "        while n > 1:\n",
    "            if n % 2 == 0:\n",
    "                n //= 2\n",
    "                res += n\n",
    "            else:\n",
    "                res += n // 2\n",
    "                n = (n + 1) // 2 \n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfMatches(self, n: int) -> int:\n",
    "        res = 0\n",
    "        while n != 1:\n",
    "            if n & 1:\n",
    "                res += n >> 1\n",
    "                n = (n >> 1) + 1\n",
    "            else:\n",
    "                res += n >> 1\n",
    "                n = n >> 1\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfMatches(self, n: int) -> int:\n",
    "        return n - 1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfMatches(self, n: int) -> int:\n",
    "        return n - 1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfMatches(self, n: int) -> int:\n",
    "        num = 0\n",
    "        while n >=2:\n",
    "            if n%2 == 0:\n",
    "                num = num+n/2\n",
    "                n = n/2\n",
    "            elif n%2 !=0:\n",
    "                num = num+(n-1)/2\n",
    "                n = (n-1)/2+1\n",
    "        return int(num)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfMatches(self, n: int) -> int:\n",
    "        if n == 2:\n",
    "            return 1\n",
    "        elif n < 2:\n",
    "            return 0\n",
    "        else:\n",
    "            if n % 2 == 0:\n",
    "                return int(self.numberOfMatches(n / 2) + n / 2)\n",
    "            else:\n",
    "                return int(self.numberOfMatches((n + 1) / 2) + (n - 1) / 2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfMatches(self, n: int) -> int:\n",
    "\n",
    "        return n-1\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfMatches(self, n: int) -> int:\n",
    "       \n",
    "        return n-1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfMatches(self, n: int) -> int:\n",
    "        return n-1     \n",
    "          \n",
    "               \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfMatches(self, n: int) -> int:\n",
    "        if n == 1:\n",
    "            return 0\n",
    "\n",
    "        res = 0\n",
    "        while n != 1:\n",
    "            if n & 1:\n",
    "                res += n >> 1\n",
    "                n = (n >> 1) + 1\n",
    "            else:\n",
    "                res += n >> 1\n",
    "                n = n >> 1\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfMatches(self, n: int) -> int:\n",
    "        total=0\n",
    "        while n>1:\n",
    "            total+=n//2\n",
    "            n=n-n//2\n",
    "        return total"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfMatches(self, n: int) -> int:\n",
    "        return n - 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfMatches(self, n: int) -> int:\n",
    "        ans = 0\n",
    "        while n != 1:\n",
    "            ans += n // 2\n",
    "            n = n // 2 + n % 2\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfMatches(self, n: int) -> int:\n",
    "        res = 0\n",
    "        while n != 1:\n",
    "            if n & 1:\n",
    "                res += n >> 1\n",
    "                n = (n >> 1) + 1\n",
    "            else:\n",
    "                res += n >> 1\n",
    "                n = n >> 1\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfMatches(self, n: int) -> int:\n",
    "        # res = 0\n",
    "        # while n > 1:\n",
    "        #     res += n // 2\n",
    "        #     n = (n + 1) // 2\n",
    "        # return res\n",
    "        return n - 1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfMatches(self, n: int) -> int:\n",
    "        return n-1\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfMatches(self, n: int) -> int:\n",
    "        res = 0\n",
    "        while n > 1:\n",
    "            res += n // 2\n",
    "            n = (n + 1) // 2\n",
    "        return res "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfMatches(self, n: int) -> int:\n",
    "        res = 0\n",
    "        while n > 1:\n",
    "            res += n // 2\n",
    "            n = (n + 1) // 2\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfMatches(self, n: int) -> int:\n",
    "        count = 0\n",
    "        while n > 1:\n",
    "            if n%2==0:\n",
    "                count+=n/2\n",
    "                n=n/2\n",
    "            else:\n",
    "                count+=(n-1)/2\n",
    "                n = (n-1)/2+1\n",
    "        return int(count)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfMatches(self, n: int) -> int:\n",
    "        count=0\n",
    "        while n>1:\n",
    "            if n%2==0:\n",
    "                count+=n/2\n",
    "                n=n/2\n",
    "            else:\n",
    "                count+=(n-1)/2\n",
    "                n=(n-1)/2+1\n",
    "        return int(count)        \n",
    "          \n",
    "               \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfMatches(self, n: int) -> int:\n",
    "        num1 = 0\n",
    "        while n != 1:\n",
    "            if n%2 == 0:\n",
    "                n = n/2\n",
    "                num1 += n\n",
    "            if n%2 == 1:\n",
    "                num1 += (n-1)/2\n",
    "                n = (n-1)/2+1    \n",
    "        return int(num1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfMatches(self, n: int) -> int:\n",
    "        return n-1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfMatches(self, n: int) -> int:\n",
    "        game = 0\n",
    "        while n > 1:\n",
    "            if n % 2 == 0:\n",
    "                game += n / 2\n",
    "                n /= 2\n",
    "            else:\n",
    "                game += (n-1)/2\n",
    "                n = (n-1)/2 + 1\n",
    "        game = int(game)\n",
    "        return(game)\n",
    "\n",
    "            \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfMatches(self, n: int) -> int:\n",
    "        ans = 0\n",
    "        while n > 1:\n",
    "            ans += n // 2\n",
    "            n -= n // 2\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfMatches(self, n: int) -> int:\n",
    "        ans = 0\n",
    "        while n > 1:\n",
    "            if n % 2 == 0:\n",
    "                ans += n // 2\n",
    "                n //= 2\n",
    "            else:\n",
    "                ans += (n - 1) // 2\n",
    "                n = (n - 1) // 2 + 1\n",
    "        return ans\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfMatches(self, n: int) -> int:\n",
    "        ans = 0\n",
    "        while n > 1:\n",
    "            time = n // 2\n",
    "            ans += time\n",
    "            n = n - time\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfMatches(self, n: int) -> int:\n",
    "        return n - 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfMatches(self, n: int) -> int:\n",
    "        i=0\n",
    "        m=n\n",
    "        while m!=1:\n",
    "            if m%2==0:\n",
    "                i=i+m/2\n",
    "                m=m/2\n",
    "            else:\n",
    "                i=i+m//2\n",
    "                m=m//2+1\n",
    "        return int(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfMatches(self, n: int) -> int:\n",
    "        return n-1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def __init__(self):\n",
    "        self.count = 0\n",
    "\n",
    "    def numberOfMatches(self, n: int) -> int:\n",
    "        return n-1\n",
    "\n",
    "        \n",
    "\n",
    "\n",
    "    \n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfMatches(self, n: int) -> int:\n",
    "        return n-1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfMatches(self, n: int) -> int:\n",
    "        count=0\n",
    "        while n!=1:\n",
    "            if n%2==0:\n",
    "                count=count+n/2\n",
    "                n=n/2\n",
    "            elif n%2==1:\n",
    "                count=count+(n-1)/2\n",
    "                n=(n+1)/2\n",
    "        count=int(count)        \n",
    "        return count\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfMatches(self, n: int) -> int:\n",
    "        c=0\n",
    "        while True:\n",
    "            if n%2==0: #如果是偶数\n",
    "               a=n/2   #配对次数\n",
    "               b=n/2   #晋级队伍\n",
    "            else:      #如果是奇数\n",
    "               a=(n-1)/2\n",
    "               b=(n-1)/2+1\n",
    "            n-=a\n",
    "            c=c+a\n",
    "            if b==1:\n",
    "                return int(c)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfMatches(self, n: int) -> int:\n",
    "        result = 0\n",
    "        if n == 1:\n",
    "            return 0\n",
    "        while n != 1:\n",
    "            if n % 2 == 0:\n",
    "                result += n // 2\n",
    "                n = n // 2\n",
    "            else:\n",
    "                result += n // 2\n",
    "                n = n // 2 + 1\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfMatches(self, n: int) -> int:\n",
    "        return n-1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfMatches(self, n: int) -> int:\n",
    "        return n - 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfMatches(self, n: int) -> int:\n",
    "        sum = 0\n",
    "        while n != 1:\n",
    "            if n % 2 == 0:\n",
    "                n = n // 2\n",
    "                sum += n\n",
    "            else:\n",
    "                n = (n-1) // 2 \n",
    "                sum += n\n",
    "                n = n + 1\n",
    "        return sum"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfMatches(self, n: int) -> int:\n",
    "        return n - 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfMatches(self, n: int) -> int:\n",
    "        return n - 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfMatches(self, n: int) -> int:\n",
    "        ans = 0\n",
    "        while n > 1:\n",
    "            i = n % 2\n",
    "            n = n // 2\n",
    "            ans += n\n",
    "            n += i\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfMatches(self, n: int) -> int:\n",
    "        return n-1\n",
    "     "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# 1688. 比赛中的配对次数\n",
    "class Solution:\n",
    "    def numberOfMatches(self, n: int) -> int:\n",
    "\n",
    "        # 结果变量\n",
    "        ans = 0\n",
    "\n",
    "        # 不断的配对比赛，直到剩下一支队伍为止\n",
    "        while n > 1:\n",
    "\n",
    "            # 1、偶数支队伍\n",
    "            if n % 2 == 0:\n",
    "\n",
    "                # 总共进行 n / 2 场比赛\n",
    "                ans += n // 2\n",
    "\n",
    "                # 同时剩下了 n / 2 支队伍\n",
    "                n //= 2\n",
    "\n",
    "            # 2、奇数支队伍\n",
    "            else:\n",
    "                # 总共进行 （ n - 1 ） / 2 场比赛\n",
    "                ans += (n - 1) // 2\n",
    "\n",
    "                # 同时剩下了 (n - 1) / 2 + 1 支队伍\n",
    "                n = (n - 1) // 2 + 1\n",
    "\n",
    "        # 获取结果\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfMatches(self, n: int) -> int:\n",
    "        count=0\n",
    "        while 1<n<=200:\n",
    "            if n%2!=0:\n",
    "                count+=(n-1)//2\n",
    "                n=(n-1)//2+1\n",
    "            else:\n",
    "                count+=n//2\n",
    "                n=n//2\n",
    "            if n==1:\n",
    "                break\n",
    "        return count\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfMatches(self, n: int) -> int:\n",
    "        return n-1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfMatches(self, n: int) -> int:\n",
    "        a=n\n",
    "        ans=0\n",
    "        while a !=1:\n",
    "            ans+=a//2\n",
    "            if a%2==1:\n",
    "                a=1+a//2\n",
    "            else:\n",
    "                a=a//2\n",
    "            \n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfMatches(self, n: int) -> int:\n",
    "       \n",
    "        count=0\n",
    "        while n>=2:\n",
    "            if n%2==1:\n",
    "                n=n-1\n",
    "                \n",
    "                count +=n/2\n",
    "                n=n//2+1\n",
    "            else:\n",
    "                count += n/2\n",
    "                n=n//2\n",
    "                \n",
    "        return int(count)\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfMatches(self, n: int) -> int:\n",
    "        return n-1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfMatches(self, n: int) -> int:\n",
    "        ans = 0\n",
    "        while n > 1:\n",
    "            ans += int(n / 2)\n",
    "            n = n-int(n / 2)\n",
    "            print(n)\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfMatches(self, n: int) -> int:\n",
    "        return n-1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfMatches(self, n: int) -> int:return n-1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfMatches(self, n: int) -> int:\n",
    "        count = 0\n",
    "        while n > 1:\n",
    "            if n % 2 == 0:\n",
    "                count += n//2\n",
    "                n//=2\n",
    "            else:\n",
    "                count += (n-1)//2\n",
    "                n = (n-1)//2+1\n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfMatches(self, n: int) -> int:\n",
    "        return n-1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfMatches(self, n: int) -> int:\n",
    "        a=0\n",
    "        while n>1:\n",
    "            if n%2==0:\n",
    "                a+=n//2\n",
    "                n=n//2\n",
    "            else:\n",
    "                a+=n//2\n",
    "                n=n//2+1\n",
    "        return a\n",
    "                 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def numberOfMatches(self, n: int) -> int:\n",
    "        res = 0\n",
    "        while n>1:\n",
    "            if n%2 == 0:\n",
    "                res += n//2\n",
    "                n = n//2\n",
    "            else:\n",
    "                res += (n-1)//2\n",
    "                n = (n-1)//2 + 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfMatches(self, n: int) -> int:\n",
    "        ret=0\n",
    "        while n>1:\n",
    "            ret+=n//2\n",
    "            n=n//2+n%2\n",
    "        return ret"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfMatches(self, n: int) -> int:\n",
    "        def countMatches(n: int) -> int:\n",
    "            if n == 1:\n",
    "                return 0\n",
    "            elif n % 2 == 0:\n",
    "                return n // 2 + countMatches(n // 2)\n",
    "            else:\n",
    "                return (n - 1) // 2 + countMatches((n - 1) // 2 + 1)\n",
    "\n",
    "        return countMatches(n)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def numberOfMatches(self, n: int) -> int:\n",
    "        res = 0\n",
    "        while n>1:\n",
    "            if n%2 == 0:\n",
    "                res += n//2\n",
    "                n = n//2\n",
    "            else:\n",
    "                res += (n-1)//2\n",
    "                n = (n-1)//2 + 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfMatches(self, n: int) -> int:\n",
    "        return n-1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfMatches(self, n: int) -> int:\n",
    "        def countMaches(n:int) -> int:\n",
    "             if n==1:\n",
    "                return 0\n",
    "             elif n % 2==0:\n",
    "                return n//2+countMaches(n//2)\n",
    "             else:\n",
    "                return (n-1)//2+countMaches((n-1)//2+1)\n",
    "        return countMaches(n)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfMatches(self, n: int) -> int:\n",
    "        return n-1\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfMatches(self, n: int) -> int:\n",
    "        if n == 1:\n",
    "            return 0\n",
    "        ans = 0\n",
    "        while n != 1:\n",
    "            ans += n//2\n",
    "            n = math.ceil(n/2)\n",
    "            \n",
    "        return ans \n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfMatches(self, n: int) -> int:\n",
    "        ans = 0\n",
    "        while n > 1:\n",
    "            if n % 2 == 0:\n",
    "                ans += n // 2\n",
    "                n //= 2\n",
    "            else:\n",
    "                ans += (n - 1) // 2\n",
    "                n = (n - 1) // 2 + 1\n",
    "        return ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfMatches(self, n: int) -> int:\n",
    "        ans = 0\n",
    "        while n != 1:\n",
    "            if n % 2 == 0:\n",
    "                ans += n // 2\n",
    "                n /= 2\n",
    "            else:\n",
    "                ans += (n-1) // 2\n",
    "                n = 1 + (n-1) //2\n",
    "        return int(ans)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfMatches(self, n: int) -> int:\n",
    "        res = 0\n",
    "        while n>1:\n",
    "            if n%2 == 0:\n",
    "                res += n//2\n",
    "                n = n//2\n",
    "            else:\n",
    "                res += (n-1)//2\n",
    "                n = (n-1)//2 + 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfMatches(self, n: int) -> int:\n",
    "        ans = 0\n",
    "        while n > 1:\n",
    "            if n % 2 == 0:\n",
    "                ans += n//2\n",
    "                n = n//2\n",
    "            else:\n",
    "                ans += n//2\n",
    "                n = (n+1)//2\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfMatches(self, n: int) -> int:\n",
    "        c=0\n",
    "        while n!=1:\n",
    "            c+=n//2\n",
    "            n=n//2+n%2\n",
    "        return c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfMatches(self, n: int) -> int:\n",
    "        v=0\n",
    "        while(n>1):\n",
    "            \n",
    "            v+=int(n/2)\n",
    "            if n%2==0:\n",
    "                n=int(n/2)\n",
    "            else:\n",
    "                n=int(n/2)+1\n",
    "        return v\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfMatches(self, n: int) -> int:\n",
    "        return n-1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfMatches(self, n: int) -> int:\n",
    "        return n-1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfMatches(self, n: int) -> int:\n",
    "        c=0\n",
    "        while n>1:\n",
    "            if n%2==0:\n",
    "                c+=n//2\n",
    "                n//=2\n",
    "            else:\n",
    "                c+=(n-1)//2\n",
    "                n=(n-1)//2+1\n",
    "        return c\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfMatches(self, n: int) -> int:\n",
    "        return n-1"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
